// pages/myjxs/jxs.js
let util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:"1", // 1 2 3 行业 企业 个人
    curLists:[],
    hyLists:[],
    qyLists:[],
    grLists:[]
  },
  showStatus:function (e) {
    let that = this;
    let status = that.data.status;
    if (e){
      if (status == e.currentTarget.dataset.status){
        return
      }
      else{
        status = e.currentTarget.dataset.status
      }
    }
    let lists = [];
    switch(status){
      case "1":lists = that.data.hyLists;break;
      case "2":lists = that.data.qyLists;break;
      case "3":lists = that.data.grLists;break;
    }
    this.setData({
      status: status,
      curLists: lists
    })
  },
  addTodoShow(){
    let that = this
    if(this.data.status == 1){
      that.tohyapply();
    }else if(this.data.status == 2){
      that.toqyapply();
    }else{
      that.tograpply();
    }
  },
  onLoad: function (options) {
    
  },

  getHyJxs(){
    let that = this;
    util.post({
      url: 'https://groomtea.icebartech.com/api/miniApp/salesman/myDealer',
      dataForm: {
        dealerType: 'industry_dealer'
      },
      success: (res) => {
        console.log(res)
        let Lists = res.data.bussData;
        Lists.sort(function (a, b) {
          return b.sumMoney - a.sumMoney;
        })
        that.data.hyLists = Lists;
        that.setData({
          status: that.data.status
        })
        that.showStatus()
      }
    });
  },
  getQyJXS(){
    let that = this;
    util.post({
      url: 'https://groomtea.icebartech.com/api/miniApp/salesman/myDealer',
      dataForm: {
        dealerType: 'enterprise_dealer'
      },
      success: (res) => {
        console.log(res)
        let Lists = res.data.bussData;
        Lists.sort(function (a, b) {
          return b.sumMoney - a.sumMoney;
        })
        that.data.qyLists = Lists;
      }
    });
  },

  getGrJxs(){
    let that = this;
    util.post({
      url: 'https://groomtea.icebartech.com/api/miniApp/salesman/myDealer',
      dataForm: {
        dealerType: 'individual_dealer'
      },
      success: (res) => {
        console.log(res)
        let Lists = res.data.bussData;
        Lists.sort(function (a, b) {
          return b.sumMoney - a.sumMoney;
        })
        that.data.grLists = Lists
      }
    })
  },

  getScale(){  //获取经销商规模
    let that = this
    util.post({
      url: "https://groomtea.icebartech.com/api/miniApp/salesman/scaleSize",
      header: { 'content-type': 'application/json' },
      success: (res) => {
        console.log('fileapply----', res)
        that.setData({
          res: res.data.bussData,
        })
      },
      fail: () => {
        wx.showToast({
          title: "请求失败，请联系管理人员",
          duration: 1500,
          icon: "none"
        })
      }
    })
  },
  onShow: function () {let that = this;
    wx.showLoading({
      title:"载入中",
      mask:true,
      success:()=>{
        that.getGrJxs();
        that.getQyJXS();
        that.getHyJxs();
        that.getScale();
        that.setData({
          status:that.data.status
        })
        setTimeout(() => {
          wx.hideLoading() 
        }, 300);
      }  
    })   
    
  },

  seeRecords(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../seeJxsRecords/seeJxsRecords?id='+id,
    })
  },

  tohyapply: function () {
    let max = this.data.res.industryDealerScale || 0
    let used = this.data.res.exploitIndustry || 0
    if (used >= max) {
      wx.showModal({
        title: '提示',
        content: '您的行业经销商名额已使用完,请先申请增加使用规模。',
        showCancel: false,
        confirmColor: "#C21D20"
      })
      return;
    }
    wx.navigateTo({
      url: `../hyapply/hyapply?max= ${max}&used=${used}`,
    })
  },
  toqyapply: function () {
    let max = this.data.res.enterpriseDealerScale || 0
    let used = this.data.res.exploitEnterprise || 0
    if (used >= max) {
      wx.showModal({
        title: '提示',
        content: '您的企业经销商名额已使用完,请先申请增加使用规模。',
        showCancel: false,
        confirmColor: "#C21D20"
      })
      return;
    }
    wx.navigateTo({
      url: `../qyapply/qyapply?max=${max}&used=${used}`,
    })
  },
  tograpply: function () {
    let max = this.data.res.individualDealerScale || 0
    let used = this.data.res.exploitIndividual || 0
    if (used >= max) {
      wx.showModal({
        title: '提示',
        content: '您的个人经销商名额已使用完,请先申请增加使用规模。',
        showCancel: false,
        confirmColor: "#C21D20"
      })
      return;
    }
    wx.navigateTo({
      url: `../grapply/grapply?max=${max}&used=${used}`,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})